﻿using Newtonsoft.Json;
using NewZealandCoffee.model;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;

namespace NewZealandCoffee
{
    public class CoffeeMakes
    {
        public bool bol = false;
        private static CoffeeMakes instance;
        private CoffeeMakes() { }

        public static CoffeeMakes Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new CoffeeMakes();
                }
                return instance;
            }
        }
        public bool InServiceMenuBol = false;
        /// <summary>
        /// 图片转base64
        /// </summary>
        /// <param name="path">图片地址</param>
        /// <returns></returns>
        public static string SetImg(string path)
        {
            return "image/jpeg;base64," + Convert.ToBase64String(System.IO.File.ReadAllBytes(path));
        }
        public void MakeCoffeeNow(string coffeename)
        {
            try
            {
                switch (coffeename)
                {
                    case "5001":
                    case "5101":
                    case "5201":
                    case "5301":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.espresso);
                        break;
                    case "5002":
                    case "5102":
                    case "5202":
                    case "5302":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.americano);
                        break;
                    case "5003":
                    case "5103":
                    case "5203":
                    case "5303":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.cappuccino);
                        break;
                    case "5004":
                    case "5104":
                    case "5204":
                    case "5304":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.Espresso2);
                        break;
                    case "5005":
                    case "5105":
                    case "5205":
                    case "5305":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.latte);
                        break;
                    case "5006":
                    case "5106":
                    case "5206":
                    case "5306":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.hotmilk);
                        break;
                    case "5007":
                    case "5107":
                    case "5207":
                    case "5307":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.hotfoam);
                        break;
                    case "5008":
                    case "5108":
                    case "5208":
                    case "5308":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.water);
                        break;
                    case "5009":
                    case "5109":
                    case "5209":
                    case "5309":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.flatwhite);
                        break;
                    case "5010":
                    case "5110":
                    case "5210":
                    case "5310":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.macchiato);
                        break;
                    case "5011":
                    case "5111":
                    case "5211":
                    case "5311":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.lattemacchiato);
                        break;
                    case "5012":
                    case "5112":
                    case "5212":
                    case "5312":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.potcoffee);
                        break;
                    case "5013":
                    case "5113":
                    case "5213":
                    case "5313":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.cremacoffee);
                        break;
                    case "5014":
                    case "5114":
                    case "5214":
                    case "5314":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.AmericanIce);
                        break;
                    case "5015":
                    case "5115":
                    case "5215":
                    case "5315":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.espresso);
                        break;
                    case "5016":
                    case "5116":
                    case "5216":
                    case "5316":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.espresso);
                        break;
                    case "5017":
                    case "5117":
                    case "5217":
                    case "5317":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.espresso);
                        break;
                    case "5018":
                    case "5118":
                    case "5218":
                    case "5318":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.espresso);
                        break;
                    case "5019":
                    case "5119":
                    case "5219":
                    case "5319":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.espresso);
                        break;
                    case "5020":
                    case "5120":
                    case "5220":
                    case "5320":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.espresso);
                        break;
                    case "5021":
                    case "5121":
                    case "5221":
                    case "5321":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.espresso);
                        break;
                    case "5022":
                    case "5122":
                    case "5222":
                    case "5322":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.espresso);
                        break;
                    case "5023":
                    case "5123":
                    case "5223":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.espresso);
                        break;
                    case "5024":
                    case "5124":
                    case "5224":
                    case "5324":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.espresso);
                        break;
                    case "5025":
                    case "5125":
                    case "5225":
                    case "5325":
                        CoffeeSerialPort.Instance.SerialSend(CoffeeMake.espresso);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 获取网络图片转换为base64
        /// </summary>
        /// <param name="url">网络图片路径   例：http://123.123.123.123/a.jpg</param>
        /// <returns></returns>
        public string GetUrlToBase64(string url)
        {
#pragma warning disable CS0168 // 声明了变量，但从未使用过
            try
            {
                var base64String = "";
                HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
                HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
                string base64sstr = string.Empty;
                using (Stream stream = webResponse.GetResponseStream())
                {
                    var mStream = new MemoryStream();

                    byte[] buffer = new byte[1024];
                    int byteCount;
                    do
                    {
                        byteCount = stream.Read(buffer, 0, buffer.Length);
                        mStream.Write(buffer, 0, byteCount);
                    } while (byteCount > 0);

                    mStream.Seek(0, SeekOrigin.Begin);
                    base64String = Convert.ToBase64String(mStream.ToArray());
                }
                //return base64String;
                WebClient mywebclient = new WebClient();
                byte[] Bytes = mywebclient.DownloadData(url);
                string Base64string = Convert.ToBase64String(Bytes);
                return "data:image/jpeg;base64," + base64String;
            }
            catch (Exception e)
            {
                return "";
            }
#pragma warning restore CS0168 // 声明了变量，但从未使用过
        }
        /// <summary>
        /// 门升
        /// </summary>
        public void OutMealClose()
        {
            try
            {

                bool bol = false;
                Console.WriteLine("yes2");
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_start);
                Console.WriteLine("yes3");
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                Thread.Sleep(200);
                while (true)
                {
                    Console.WriteLine("yes4");
                    Console.WriteLine(WuLiaoCoffeeSel.Instance.BANNER_Sel_Bol);
                    if (!WuLiaoCoffeeSel.Instance.BANNER_Sel_Bol)
                    {
                        IOMsg.Instance.AllBol = true;
                        if (WuLiaoCoffeeSel.Instance.PushRodUP_Sel_Bol)
                        {
                            Console.WriteLine("yes7");
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(200);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                            Thread.Sleep(200);
                            IOMsg.Instance.AllBol = true;
                            bol = false;
                            break;
                        }
                        Thread.Sleep(200);
                    }
                    else
                    {
                        Console.WriteLine(DateTime.Now.ToString());
                        IOMsg.Instance.AllBol = false;
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                        Thread.Sleep(200);
                        IOMsg.Instance.AllBol = true;
                        while (true)
                        {
                            if (WuLiaoCoffeeSel.Instance.PushRodUP_Sel_Bol)
                            {
                                Console.WriteLine("yes9");
                                IOMsg.Instance.AllBol = false;
                                Thread.Sleep(500);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                                Thread.Sleep(200);
                                IOMsg.Instance.AllBol = true;
                                bol = false;
                                break;
                            }
                            if (!WuLiaoCoffeeSel.Instance.BANNER_Sel_Bol)
                            {
                                bol = true;
                                break;
                            }
                            Thread.Sleep(200);
                            Console.WriteLine(DateTime.Now.ToString() + "光栅亮");
                        }
                        if (bol)
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(200);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                            Thread.Sleep(200);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                            Thread.Sleep(200);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_start);
                            Console.WriteLine("yes3");
                            Thread.Sleep(200);
                            IOMsg.Instance.AllBol = true;
                            Thread.Sleep(200);
                        }
                    }
                    Thread.Sleep(10);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("yesno");
                return;
            }
        }
        /// <summary>
        /// 出餐门流程
        /// </summary>
        /// <param name="id"></param>
        /// <param name="code">1,冰淇淋  2，冰沙</param>
        public void MakeMealMsg(string id)
        {
            try
            {
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_start);
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                while (true)
                {
                    if (WuLiaoCoffeeSel.Instance.PushRodDown_Sel_Bol)
                    {
                        break;
                    }
                    else
                    {
                        Thread.Sleep(200);
                    }
                    Thread.Sleep(10);
                }
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                ClientList clientList = new ClientList();
                clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList.code = 10;
                clientList.IsMake = true;
                SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                Thread.Sleep(200);
                int callidex = 0;
                string ids = "";
                if (id.Length > 6)
                    id = id.Substring(id.Length - 6, 6);
                if (id.Length == 6)
                {
                    string read = id.Substring(0, 1) + " " + id.Substring(1, 1) + " " + id.Substring(2, 1) + " " + id.Substring(3, 1) + " " + id.Substring(4, 1) + " " + id.Substring(5, 1) + " ";
                    string read1 = EnglishRead(id.Substring(0, 1)) + " " + EnglishRead(id.Substring(1, 1)) + " " + EnglishRead(id.Substring(2, 1)) + " " + EnglishRead(id.Substring(3, 1)) + " " + EnglishRead(id.Substring(4, 1)) + " " + EnglishRead(id.Substring(5, 1)) + " ";
                    id = read;
                    ids = read1;
                }
                while (true)
                {
                    if (!WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol)
                    {
                        break;
                    }
                    else
                    {

                        if (callidex % 100 == 0)
                            SpeechVoice.Instance.VoicePlayback("NO. " + ids + ",Please pick up your food.");
                        Thread.Sleep(100);
                        callidex++;
                    }
                    Thread.Sleep(100);
                }
                Thread.Sleep(6000);
                SpeechVoice.Instance.VoicePlayback("The door is closing,please watch your hands.");
                Thread.Sleep(5000);
                while (true)
                {
                    if (!WuLiaoCoffeeSel.Instance.BANNER_Sel_Bol)
                    {
                        break;
                    }
                    else
                    {
                        Thread.Sleep(100);
                    }
                    Thread.Sleep(10);
                }
                Console.WriteLine("yes1");
                OutMealClose();
                Console.WriteLine("yes11");
            }
            catch (Exception)
            {
                return;
            }
        }
        public string EnglishRead(string id)
        {
            try
            {
                string name = "";
                switch (id)
                {
                    case "0":
                        name = "Zero";
                        break;
                    case "1":
                        name = "One";
                        break;
                    case "2":
                        name = "Two";
                        break;
                    case "3":
                        name = "Three";
                        break;
                    case "4":
                        name = "Four";
                        break;
                    case "5":
                        name = "Five";
                        break;
                    case "6":
                        name = "Six";
                        break;
                    case "7":
                        name = "Seven";
                        break;
                    case "8":
                        name = "Eight";
                        break;
                    case "9":
                        name = "Nine";
                        break;
                }
                return name;
            }
            catch (Exception)
            {
                return "";
            }
        }
        /// <summary>
        /// 冰淇淋制作
        /// </summary>
        public void BQLMaking()
        {
            try
            {
                IOMsg.Instance.AllBol = false;
                IOMsg.Instance.RobotBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJBQL_UP);
                Thread.Sleep(200);
                while (true)
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDBQLD);
                    Thread.Sleep(100);
                    if (IOMsg.Instance.RobotBol)
                    {
                        break;
                    }
                    Thread.Sleep(1000);
                }
                Thread.Sleep(100);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_BQL_Down);
                Thread.Sleep(100);
                IOMsg.Instance.AllBol = true;
                IceMsg.Instance.AllBol = false;
              
                Thread.Sleep(2000);
                bol = true;
                int IceInit = 0;

                ICSerialPort.Instance.SerialSend(IceMsgbool.Instance.IceChuLiaoStart);
                Console.WriteLine("发送  " + IceMsgbool.Instance.IceChuLiaoStart);
                File.WriteAllText(MainWindow.WarnPath + @"\发送" + DateTime.Now.ToString("yyyyddMMHHmmssfff") + ".txt", JsonConvert.SerializeObject(IceMsgbool.Instance.IceChuLiaoStart));            
                while (true)
                {
                    if (!bol)
                    {
                        break;
                    }
                    if (IceInit == 10)
                    {
                        ICSerialPort.Instance.SerialSend(IceMsgbool.Instance.IceChuLiaoStart);
                        Console.WriteLine("发送  " + IceMsgbool.Instance.IceChuLiaoStart);
                        File.WriteAllText(MainWindow.WarnPath + @"\发送1" + DateTime.Now.ToString("yyyyddMMHHmmssfff") + ".txt", JsonConvert.SerializeObject(IceMsgbool.Instance.IceChuLiaoStart));
                        break;
                    }
                    Thread.Sleep(1000);
                }
                int bqlIint = 0;
                while (true)
                {
                    bol = false;
                    ICSerialPort.Instance.SerialSend(IceMsgbool.Instance.IceChuLiaoOver1);
                    Thread.Sleep(500);
                    if (!IceMsg.Instance.isover)
                    {
                        break;
                    }
                    if (bqlIint == 60)
                    {
                        break;
                    }
                    bqlIint++;
                    Thread.Sleep(500);
                }
                while (true)
                {
                    ICSerialPort.Instance.SerialSend(IceMsgbool.Instance.IceChuLiaoOver1);
                    Thread.Sleep(500);
                    if (IceMsg.Instance.isover)
                    {
                        break;
                    }
                    Thread.Sleep(500);
                }
                IceMsg.Instance.AllBol = true;
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBQLWC_UP);
                Thread.Sleep(200);
            }
            catch (Exception)
            {
                return;
            }
        }

        public void XNMaking()
        {
            try
            {
                IOMsg.Instance.AllBol = false;
                IOMsg.Instance.RobotBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJXN_UP);
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = false;
                IOMsg.Instance.RobotBol = false;
                Thread.Sleep(100);
                while (true)
                {
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBSKS);
                    Thread.Sleep(100);
                    if (IOMsg.Instance.RobotBol)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }
                int bstime = 0;
                while (true)
                {
                    IOMsg.Instance.AllBol = false;
                    IOMsg.Instance.RobotBol = false;
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBSKS);
                    Thread.Sleep(300);
                    if (!IOMsg.Instance.RobotBol)
                    {
                        break;
                    }
                    else if (MainWindow.BSIint1 == bstime)
                    {
                        Thread.Sleep(200);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBSWC_UP);
                        Thread.Sleep(200);
                        break;
                    }
                    bstime++;
                    Thread.Sleep(500);
                }
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
